package leetcode.tree;

import leetcode.tree.common.TreeNode;

import java.util.Stack;

/**
 * https://leetcode-cn.com/problems/validate-binary-search-tree/
 *
 * @author mazouri
 * @create 2021-08-22 16:56
 */
public class _98_isValidBST {
    public boolean isValidBST(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        //leetcode卡边界值
        double frontValue = -Double.MAX_VALUE;

        while (cur != null || !stack.isEmpty()) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }

            cur = stack.pop();

            if (cur.val <= frontValue) return false;
            frontValue = cur.val;
            cur = cur.right;
        }

        return true;
    }
}
